package com.codingstudy.login.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * Api访问页面，Swagger
 * </p>
 *
 * @author Dudu
 * @since 2020-12-11
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger2.title}")
    private String title;
    @Value("${swagger2.description}")
    private String description;
    @Value("${swagger2.contact.name}")
    private String contactName;
    @Value("${swagger2.contact.url}")
    private String contactUrl;
    @Value("${swagger2.contact.email}")
    private String contactEmail;
    @Value("${swagger2.version}")
    private String version;

    @Value("${swagger2.enabled:true}")
    private Boolean enabled;

    @Bean
    public Docket createRestApi(){
        //return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.codingstudy.login.controller")).build();

        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization") // Updates the parameter name
                .description("JSON Web Token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                // Sets the api's meta information as included in the json ResourceListing response.
                .apiInfo(apiInfo())
                // Initiates a builder for api selection.
                .select()
                // Any RequestHandler satisfies this condition
                .apis(RequestHandlerSelectors.any())
                // Any path satisfies this condition
                .paths(PathSelectors.any())
                .build();
                // Adds default parameters which will be applied to all operations.
                //.globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }
}
